﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using PlainOldVersionControl.Domain;

namespace PlainOldVersionControl.Data.Base
{
    public class PlainSettingsDA
    {
        public static string GetHead()
        {
            return GetSettingValue(SettingType.HEAD);
        }

        public static void SetHead(string hash)
        {
            SetSettingValue(hash, SettingType.HEAD);
        }

        public static string GetRootPath()
        {
            return GetSettingValue(SettingType.RootPath);
        }

        #region Private Methods

        private static string GetSettingValue(SettingType type)
        {
            string sql = string.Format("select settingvalue from  settings where id = {0}", (int)type);
            IQuery qry = DA.GetSqlQuery(sql);
            return qry.UniqueResult().ToString();
        }

        private static void SetSettingValue(string newValue, SettingType type)
        {
            string sql = string.Format("update settings set settingvalue = '{0}' where id = {1}", newValue, (int)type);
            IQuery qry = DA.GetSqlQuery(sql);
            qry.ExecuteUpdate();
        }

        #endregion
    }
}
